<!DOCTYPE html>
<html lang="zh-CN">
  <head>
  <meta charset="UTF-8">
  <meta 
    name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta 
    http-equiv="X-UA-Compatible" 
    content="ie=edge">
  <meta 
    name="theme-color" 
    content="#fff" 
    id="theme-color">
  <meta 
    name="description" 
    content="hexoDemo用于介绍如何使用hexo">
  <link 
    rel="icon" 
    href="/">
  <title>02-开始写JavaScript：hello world</title>
  
    
      <meta 
        property="og:title" 
        content="02-开始写JavaScript：hello world">
    
    
      <meta 
        property="og:url" 
        content="https://zwl5670.gitee.io/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/02-%E5%BC%80%E5%A7%8B%E5%86%99JavaScript%EF%BC%9Ahello%20world/index.html">
    
    
      <meta 
        property="og:img" 
        content="https://img.songhn.com/img/Y67gdd.png">
    
    
      <meta 
        property="og:img" 
        content="hexoDemo用于介绍如何使用hexo">
    
    
      <meta 
        property="og:type" 
        content="article">
      <meta 
        property="og:article:published_time" 
        content="2004-03-01">
      <meta 
        property="og:article:modified_time" 
        content="2023-03-14">
      <meta 
        property="og:article:author" 
        content="zwl5670">
      
        
          <meta 
            property="og:article:tag" 
            content="JS基础">
        
      
    
  
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  <link rel="preload" href="/css/main.css" as="style" >
  
  <link rel="modulepreload" href="//instant.page/5.1.0">
  
  <link rel="stylesheet" href="/css/main.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">
  
  
  
    <link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">
  
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
    function loadCSS(href, data, attr) {
      var sheet = document.createElement('link');
      sheet.ref = 'stylesheet';
      sheet.href = href;
      sheet.dataset[data] = attr;
      document.head.appendChild(sheet);
    }
    function changeCSS(cssFile, data, attr) {
      var oldlink = document.querySelector(data);
      var newlink = document.createElement("link");
      newlink.setAttribute("rel", "stylesheet");
      newlink.setAttribute("href", cssFile);
      newlink.dataset.prism = attr;
      document.head.replaceChild(newlink, oldlink);
    }
  </script>
  
    
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
  </script>
  
    <script>
      var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
        document.getElementById('theme-color').dataset.mode = getCssMediaQuery();
      }
    };
    setDarkmode();
    </script>
  
  
  
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
    <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
<meta name="generator" content="Hexo 6.3.0"></head>

  <body>
    <div class="wrapper">
       
      <nav class="navbar">
  <div class="navbar-logo">
    <a class="navbar-logo-main" href="/">
      
      <span class="navbar-logo-dsc">前端面试</span>
      </a>
  </div>
  <div class="navbar-menu">
    
      <a 
        href="/" 
        class="navbar-menu-item">
        
          首页
        
      </a>
    
      <a 
        href="/archives" 
        class="navbar-menu-item">
        
          归档
        
      </a>
    
      <a 
        href="/tags" 
        class="navbar-menu-item">
        
          标签
        
      </a>
    
      <a 
        href="/categories" 
        class="navbar-menu-item">
        
          分类
        
      </a>
    
      <a 
        href="/about" 
        class="navbar-menu-item">
        
          关于
        
      </a>
    
      <a 
        href="/links" 
        class="navbar-menu-item">
        
          友链
        
      </a>
    
    <button 
      class="navbar-menu-item darknavbar navbar-menu-btn" 
      aria-label="Toggle dark mode"
      id="dark">
      <i class="iconfont icon-weather"></i>
    </button>
    <button 
      class="navbar-menu-item searchnavbar navbar-menu-btn" 
      aria-label="Toggle search"
      id="search">
      <!-- <i 
        class="iconfont icon-search" 
        style="font-size: 1.2rem; font-weight: 400;">
      </i> -->
      <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img"
        class="iconify iconify--ion" width="28" height="28" preserveAspectRatio="xMidYMid meet" viewBox="0 0 512 512">
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M256 80a176 176 0 1 0 176 176A176 176 0 0 0 256 80Z"></path>
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M232 160a72 72 0 1 0 72 72a72 72 0 0 0-72-72Z"></path>
        <path fill="none" stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="28"
          d="M283.64 283.64L336 336"></path>
      </svg>
    </button>
  </div>
</nav> 
      
      <div 
        id="local-search" 
        style="display: none">
        <input
          class="navbar-menu-item"
          id="search-input"
          placeholder="请输入搜索内容..." />
        <div id="search-content"></div>
      </div>
      
      <div class="section-wrap">
        <div class="container">
          <div class="columns">
            <aside class="left-column">
              
              <div class="card card-author">
                
  <img 
    src="https://img.songhn.com/img/Y67gdd.png" 
    class="author-img"
    width="88"
    height="88"
    alt="author avatar">

<p class="author-name">zwl5670</p>
<p class="author-description">前端学习资料</p>
<div class="author-message">
  <a 
    class="author-posts-count" 
    href="/archives">
    <span>215</span>
    <span>文章</span>
  </a>
  <a 
    class="author-categories-count" 
    href="/categories">
    <span>14</span>
    <span>分类</span>
  </a>
  <a 
    class="author-tags-count" 
    href="/tags">
    <span>14</span>
    <span>标签</span>
  </a>
</div>

              </div>
               <div class="sticky-tablet">
  
  
    <article class="display-when-two-columns spacer">
      <div class="card card-content toc-card">
        <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%80%E5%A7%8B%E5%86%99%E7%AC%AC%E4%B8%80%E8%A1%8C-JavaScript%EF%BC%9Ahello-world"><span class="toc-text">开始写第一行 JavaScript：hello world</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F-1%EF%BC%9A%E8%A1%8C%E5%86%85%E5%BC%8F"><span class="toc-text">方式 1：行内式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F-2%E3%80%81%E5%86%85%E5%B5%8C%E5%BC%8F%EF%BC%88%E5%86%85%E8%81%94%E5%BC%8F%EF%BC%89"><span class="toc-text">方式 2、内嵌式（内联式）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F-3%EF%BC%9A%E5%A4%96%E9%93%BE%E5%BC%8F"><span class="toc-text">方式 3：外链式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%B3%E4%BA%8E-window-onload%EF%BC%9A%E5%85%88%E5%8A%A0%E8%BD%BD%EF%BC%8C%E6%9C%80%E5%90%8E%E6%89%A7%E8%A1%8C"><span class="toc-text">关于 window.onload：先加载，最后执行</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JavaScript-%E4%B8%80%E4%BA%9B%E7%AE%80%E5%8D%95%E7%9A%84%E8%AF%AD%E6%B3%95%E8%A7%84%E5%88%99"><span class="toc-text">JavaScript 一些简单的语法规则</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%89%8D%E7%AB%AF%E4%BB%A3%E7%A0%81%E7%9A%84%E6%B3%A8%E9%87%8A"><span class="toc-text">前端代码的注释</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#HTML-%E7%9A%84%E6%B3%A8%E9%87%8A"><span class="toc-text">HTML 的注释</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#CSS-%E7%9A%84%E6%B3%A8%E9%87%8A"><span class="toc-text">CSS 的注释</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#JavaScript-%E7%9A%84%E6%B3%A8%E9%87%8A"><span class="toc-text">JavaScript 的注释</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AF%B9%E6%B3%A8%E9%87%8A%E7%9A%84%E8%AE%A4%E7%9F%A5"><span class="toc-text">对注释的认知</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JavaScript-%E8%BE%93%E5%87%BA%E8%AF%AD%E5%8F%A5"><span class="toc-text">JavaScript 输出语句</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%B9%E7%AA%97%EF%BC%9Aalert-%E8%AF%AD%E5%8F%A5"><span class="toc-text">弹窗：alert()语句</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%B9%E7%AA%97%EF%BC%9Aconfirm-%E8%AF%AD%E5%8F%A5%EF%BC%88%E5%90%AB%E7%A1%AE%E8%AE%A4-x2F-%E5%8F%96%E6%B6%88%EF%BC%89"><span class="toc-text">弹窗：confirm()语句（含确认&#x2F;取消）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%B9%E5%87%BA%E8%BE%93%E5%85%A5%E6%A1%86%EF%BC%9Aprompt-%E8%AF%AD%E5%8F%A5"><span class="toc-text">弹出输入框：prompt()语句</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BD%91%E9%A1%B5%E5%86%85%E5%AE%B9%E5%8C%BA%E5%9F%9F%E8%BE%93%E5%87%BA%EF%BC%9Adocument-write-%E8%AF%AD%E5%8F%A5"><span class="toc-text">网页内容区域输出：document.write()语句</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8E%A7%E5%88%B6%E5%8F%B0%E8%BE%93%E5%87%BA%EF%BC%9Aconsole-log-%E6%89%93%E5%8D%B0%E6%97%A5%E5%BF%97"><span class="toc-text">控制台输出：console.log() 打印日志</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%B5%9E%E8%B5%8F%E4%BD%9C%E8%80%85"><span class="toc-text">赞赏作者</span></a></li></ol>
      </div>
    </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header">
    <i 
      class="iconfont icon-fenlei" 
      style="padding-right: 2px;">
    </i>分类
  </div>
  <div class="categories-list">
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/">
        <div class="categories-list-item">
          前端工具
          <span class="categories-list-item-badge">13</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          CSS基础
          <span class="categories-list-item-badge">18</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          CSS进阶
          <span class="categories-list-item-badge">9</span>
        </div>
      </a>
    
      <a href="/categories/HTML/">
        <div class="categories-list-item">
          HTML
          <span class="categories-list-item-badge">12</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          JS基础
          <span class="categories-list-item-badge">56</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80ES6/">
        <div class="categories-list-item">
          JS基础ES6
          <span class="categories-list-item-badge">10</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/">
        <div class="categories-list-item">
          JS基础异步
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/JS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          JS进阶
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/%E7%A7%BB%E5%8A%A8web/">
        <div class="categories-list-item">
          移动web
          <span class="categories-list-item-badge">3</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%92%8CDOM/">
        <div class="categories-list-item">
          CSS和DOM
          <span class="categories-list-item-badge">6</span>
        </div>
      </a>
    
      <a href="/categories/Node/">
        <div class="categories-list-item">
          Node
          <span class="categories-list-item-badge">16</span>
        </div>
      </a>
    
      <a href="/categories/Vue%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          Vue基础
          <span class="categories-list-item-badge">15</span>
        </div>
      </a>
    
      <a href="/categories/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/">
        <div class="categories-list-item">
          性能优化
          <span class="categories-list-item-badge">7</span>
        </div>
      </a>
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/">
        <div class="categories-list-item">
          前端工程化
          <span class="categories-list-item-badge">8</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header">
    <i 
      class="iconfont icon-biaoqian" 
      style="padding-right: 2px;">
    </i>热门标签
  </div>
  <div class="tags-list">
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80/" 
        title="JS基础">
        <div class="tags-list-item">JS基础</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%9F%BA%E7%A1%80/" 
        title="CSS基础">
        <div class="tags-list-item">CSS基础</div>
      </a>
    
      <a 
        href="/tags/Node/" 
        title="Node">
        <div class="tags-list-item">Node</div>
      </a>
    
      <a 
        href="/tags/Vue%E5%9F%BA%E7%A1%80/" 
        title="Vue基础">
        <div class="tags-list-item">Vue基础</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/" 
        title="前端工具">
        <div class="tags-list-item">前端工具</div>
      </a>
    
      <a 
        href="/tags/HTML/" 
        title="HTML">
        <div class="tags-list-item">HTML</div>
      </a>
    
      <a 
        href="/tags/JS%E8%BF%9B%E9%98%B6/" 
        title="JS进阶">
        <div class="tags-list-item">JS进阶</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/" 
        title="JS基础异步">
        <div class="tags-list-item">JS基础异步</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80ES6/" 
        title="JS基础ES6">
        <div class="tags-list-item">JS基础ES6</div>
      </a>
    
      <a 
        href="/tags/CSS%E8%BF%9B%E9%98%B6/" 
        title="CSS进阶">
        <div class="tags-list-item">CSS进阶</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/" 
        title="前端工程化">
        <div class="tags-list-item">前端工程化</div>
      </a>
    
      <a 
        href="/tags/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" 
        title="性能优化">
        <div class="tags-list-item">性能优化</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%92%8CDOM/" 
        title="CSS和DOM">
        <div class="tags-list-item">CSS和DOM</div>
      </a>
    
      <a 
        href="/tags/%E7%A7%BB%E5%8A%A8web/" 
        title="移动web">
        <div class="tags-list-item">移动web</div>
      </a>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
            <main class="main-column">
              
<article class="card card-content">
  <header>
    <h1 class="post-title">
      02-开始写JavaScript：hello world
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2004-02-29T16:00:00.000Z">
      <i 
        class="iconfont icon-calendar" 
        style="margin-right: 2px;">
      </i>
      <span>2004-03-01</span>
    </time>
    
      <span class="dot"></span>
      
        <a 
          href="/categories/JS%E5%9F%BA%E7%A1%80/" 
          class="post-meta-link">
          JS基础
        </a>
      
    
    
      <span class="dot"></span>
      <span>3.1k 字</span>
    
  </div>
  
    <div 
      class="post-meta post-show-meta" 
      style="margin-top: -10px;">
      <div style="display: flex; align-items: center;">
        <i 
          class="iconfont icon-biaoqian" 
          style="margin-right: 2px; font-size: 1.15rem;">
        </i>
        
          
          <a 
            href="/tags/JS%E5%9F%BA%E7%A1%80/" 
            class="post-meta-link">
            JS基础
          </a>
        
      </div>
    </div>
  
  </header>
  <div 
    id="section" 
    class="post-content">
    <h2 id="开始写第一行-JavaScript：hello-world"><a href="#开始写第一行-JavaScript：hello-world" class="headerlink" title="开始写第一行 JavaScript：hello world"></a>开始写第一行 JavaScript：hello world</h2><p>JS 代码的书写位置在哪里呢？这个问题，也可以理解成：引入 JS 代码，有哪几种方式？有三种方式：（和 CSS 的引入方式类似）</p>
<ol>
<li><p><strong>行内式</strong>：写在标签内部。</p>
</li>
<li><p><strong>内嵌式</strong>（内联式）：写在 head 标签中。</p>
</li>
<li><p><strong>外链式</strong>：引入外部 JS 文件。</p>
</li>
</ol>
<h3 id="方式-1：行内式"><a href="#方式-1：行内式" class="headerlink" title="方式 1：行内式"></a>方式 1：行内式</h3><p><strong>代码举例</strong>：</p>
<pre class="highlight"><span class="line">&lt;input type=<span class="string">&quot;button&quot;</span> value=<span class="string">&quot;点我点我&quot;</span> onclick=<span class="string">&quot;alert(&#x27;千古壹号 Hello 方式1&#x27;)&quot;</span> /&gt;</span><br></pre>

<p>完整的可执行代码如下：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">&quot;viewport&quot;</span> <span class="attr">content</span>=<span class="string">&quot;width=device-width, initial-scale=1.0&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;button&quot;</span> <span class="attr">value</span>=<span class="string">&quot;点我点我&quot;</span> <span class="attr">onclick</span>=<span class="string">&quot;alert(&#x27;千古壹号 Hello 方式1&#x27;)&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>

<p><strong>分析</strong>：</p>
<ul>
<li><p>可以将单行或少量 JS 代码写在 HTML 标签的事件属性中（以 on 开头的属性），比如放在上面的 <code>onclick</code>点击事件中。</p>
</li>
<li><p>这种书写方式，不推荐使用，原因是：可读性差，尤其是需要编写大量 JS 代码时，很难维护；引号多层嵌套时，也容易出错。</p>
</li>
<li><p>关于代码中的「引号」，在 HTML 标签中，我们推荐使用双引号，JS 中我们推荐使用单引号。</p>
</li>
</ul>
<h3 id="方式-2、内嵌式（内联式）"><a href="#方式-2、内嵌式（内联式）" class="headerlink" title="方式 2、内嵌式（内联式）"></a>方式 2、内嵌式（内联式）</h3><p>我们可以在 HTML 页面的 <code>&lt;body&gt;</code> 标签里放入<code>&lt;script type=”text/javascript”&gt;&lt;/script&gt;</code>标签对，并在<code>&lt;script&gt;</code>里书写 JavaScript 代码：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">// 在这里写 js 代码</span></span></span><br><span class="line"><span class="language-javascript">            <span class="title function_">alert</span>(<span class="string">&#x27;千古壹号 hello 方式2&#x27;</span>);</span></span><br><span class="line"><span class="language-javascript">            <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;qianguyihao hello 方式2&#x27;</span>);</span></span><br><span class="line"><span class="language-javascript">        </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>

<p><strong>分析</strong>：</p>
<ul>
<li><p>text 表示纯文本，因为 JavaScript 代码本身就是纯文本。</p>
</li>
<li><p>可以将多行 JS 代码写到 <code>&lt;script&gt;</code> 标签中。</p>
</li>
<li><p>内嵌式 JS 是学习时常用的方式。</p>
</li>
</ul>
<h3 id="方式-3：外链式"><a href="#方式-3：外链式" class="headerlink" title="方式 3：外链式"></a>方式 3：外链式</h3><pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 外链式：引入外部的 js 文件：这个 utils.js 文件与当前的 html 文件，处于同一级目录 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;utils.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>

<p><strong>分析</strong>：</p>
<ul>
<li>上面这段代码，依然是放到 body 标签里，可以和内嵌的 JS 代码并列。</li>
<li>上方代码的 script 标签已经引入了外部 JS 文件，所以这个标签里面，不可以再写 JS 代码。</li>
</ul>
<p><strong>总结</strong>：</p>
<p>我们在实战开发中，基本都是采用方式 3，因为将 html 文件和 js 文件分开的方式，有利于代码的结构化和复用，符合高内聚、低耦合的思想。很少会有人把一大堆 JS 代码塞到 html 文件里。</p>
<h3 id="关于-window-onload：先加载，最后执行"><a href="#关于-window-onload：先加载，最后执行" class="headerlink" title="关于 window.onload：先加载，最后执行"></a>关于 window.onload：先加载，最后执行</h3><p>上面的三种方式，有个共同的地方是：JS 代码都是写在<code>&lt;/body&gt;</code>中的，准确来说，是在页面标签元素的后面，在 body<strong>结束标签</strong>的前面。</p>
<p>为什么一般是按这样的顺序来写呢？这是因为：浏览器默认会<strong>从上至下</strong>解析网页（这句话很重要）。当你<strong>需要通过 JS 来操作界面上的标签元素</strong>的时候，假如将 JS 代码、<code>&lt;script&gt;</code>标签写到<code>&lt;head&gt;</code>标签中，或者写在页面标签元素的前面，那么这样的 JS 是无效的，因为标签元素在此时都还没来得及加载，自然无法操作这个元素。</p>
<p><strong>重点来了：</strong></p>
<p>当你<strong>需要通过 JS 来操作界面上的标签元素</strong>的时候，如果实在想把 JS 写到<code>&lt;head&gt;</code>标签中，那么就必须用 window.onload 将 JS 代码进行包裹。代码格式如下：</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">  window.onload = function()&#123;</span><br><span class="line">    // 这里可以写操作界面元素的JS代码，等页面加载完毕后再执行</span><br><span class="line">    ...</span><br><span class="line">  &#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br></pre>

<p><strong>window.onload</strong>的含义是：等界面上所有内容都加载完毕后（不仅要等文本加载完毕，而且要等图片也要加载完毕），再执行<code>&#123;&#125;</code>中的代码。做到了<strong>先加载，最后执行</strong>，也就是：<strong>等页面加载完毕后再执行</strong>。</p>
<p>当然，我们可以根据具体需求，将 window.onload 写在 <code>&lt;head&gt;</code>标签中，或者写在<code>&lt;script&gt;</code>标签中。</p>
<h2 id="JavaScript-一些简单的语法规则"><a href="#JavaScript-一些简单的语法规则" class="headerlink" title="JavaScript 一些简单的语法规则"></a>JavaScript 一些简单的语法规则</h2><p>学习程序，是有规律可循的，程序会有有相同的部分，这些部分就是一种规定，不能更改，我们称之为：语法。我们先来了解一个简单的语法规则。</p>
<p>1、JS 对换行、缩进、空格不敏感。每一条语句以分号结尾。</p>
<p>也就是说：</p>
<p>代码一：</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="title function_">alert</span>(<span class="string">&#x27;今天蓝天白云&#x27;</span>);</span></span><br><span class="line"><span class="language-javascript">    <span class="title function_">alert</span>(<span class="string">&#x27;我很高兴&#x27;</span>);</span></span><br><span class="line"><span class="language-javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre>

<p>等价于代码二：</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="title function_">alert</span>(<span class="string">&#x27;今天蓝天白云&#x27;</span>);<span class="title function_">alert</span>(<span class="string">&#x27;我很高兴&#x27;</span>);</span></span><br><span class="line"><span class="language-javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre>

<p>2、每一条语句末尾要加上<strong>分号</strong>。虽然分号不是必须加的，但如果不写分号，浏览器会自动添加分号，导致消耗一些系统资源和性能，甚至有可能<strong>添加错误</strong>。</p>
<p>3、所有的符号，都是英文的。比如<strong>括号</strong>、引号、分号。</p>
<p>如果你用的是搜狗拼音，<strong>建议不要用 shift 切换中英文</strong>（可以在搜狗软件里进行设置），不然很容易输入中文的分号；建议用 ctrl+space 切换中英文输入法。</p>
<p>4、JS 严格区分大小写。</p>
<h2 id="前端代码的注释"><a href="#前端代码的注释" class="headerlink" title="前端代码的注释"></a>前端代码的注释</h2><p>注释：即解释、注解。注释有利于提高代码的可读性，且有利于程序员之间的沟通。</p>
<p>注释可以用来解释某一段代码的功能和作用；通过注释，还可以补充代码中未体现出来的部分。</p>
<p>注释可以是任何文字，可以写中文。</p>
<p><strong>我们不要把 HTML、CSS、JavaScript 三者的注释格式搞混淆了</strong>。</p>
<h3 id="HTML-的注释"><a href="#HTML-的注释" class="headerlink" title="HTML 的注释"></a>HTML 的注释</h3><p>格式：</p>
<pre class="highlight"><span class="line"><span class="comment">&lt;!-- 我是 HTML 注释  --&gt;</span></span><br></pre>

<p>举例：</p>
<pre class="highlight"><span class="line"><span class="comment">&lt;!--头部开始--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;header&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--头部结束--&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!--内容开始--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;main&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--内容结束--&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!--底部开始--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;footer&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--底部结束--&gt;</span></span><br></pre>

<h3 id="CSS-的注释"><a href="#CSS-的注释" class="headerlink" title="CSS 的注释"></a>CSS 的注释</h3><p>举例：</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">&quot;text/css&quot;</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">    <span class="comment">/* 我是 CSS 注释 */</span></span></span><br><span class="line"><span class="language-css">    <span class="selector-tag">p</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">font-weight</span>: bold;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">font-style</span>: italic;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">color</span>: red;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre>

<p>注意：CSS 只有<code>/* */</code>这种注释，没有<code>//</code>这种注释。而且注释要写在<code>&lt;style&gt;</code>标签里面才算生效。</p>
<h3 id="JavaScript-的注释"><a href="#JavaScript-的注释" class="headerlink" title="JavaScript 的注释"></a>JavaScript 的注释</h3><p>单行注释：</p>
<pre class="highlight"><span class="line"><span class="comment">// 我是注释</span></span><br></pre>

<p>多行注释：</p>
<pre class="highlight"><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">	多行注释1</span></span><br><span class="line"><span class="comment">	多行注释2</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre>

<p>补充：VS Code 中，单行注释的快捷键是「Ctrl + &#x2F;」，多行注释的默认快捷键是「Alt + Shift + A」。</p>
<p>当然，如果你觉得多行注释的默认快捷键不方便，我们还可以修改默认快捷键。操作如下：</p>
<p>VS Code –&gt; 首选项 –&gt; 键盘快捷方式 –&gt; 查找“注释”这两个字 –&gt; 将原来的快捷键修改为其他的快捷键，比如「Ctrl + Shift + &#x2F;」。</p>
<h3 id="对注释的认知"><a href="#对注释的认知" class="headerlink" title="对注释的认知"></a>对注释的认知</h3><p>很多人认为：代码注释是多余的。他们的理由是：如果注释太多，说明代码写得不清晰；而且，代码更新的同时，如果注释没更新，那段注释就成了磁盘上的垃圾，误导他人。</p>
<p>还有人认为：注释应该尽可能详细，就像写小作文一样。</p>
<p>上面这样的理由，都不具有说服力。我告诉你为什么要写注释：</p>
<ol>
<li><p>所有注释都是必要的吗？当然不是。注释不应该用来解释某些代码正在做什么。如果代码无法清楚到去解释自己时，那么代码需要变得更简单。有一些例外，比如正则表达式和复杂算法通常会从解释他们正在做什么事情的注释中获益很多。</p>
</li>
<li><p>注释用于解释为什么某些代码存在时很有用。大多数注释都是针对代码本身无法包含的信息，例如决策背后的推理、业务流程、业务逻辑、注意事项、防踩坑指南、参考链接。</p>
</li>
<li><p>注释即文档，需要持续更新维护，新陈代谢。文档也会过期，但不能因噎废食。即便文档过期，至少它记载了曾经的开发记录。</p>
</li>
</ol>
<p>推荐阅读：</p>
<ul>
<li>开发者代码审查 review 指南：<a target="_blank" rel="noopener" href="https://jimmysong.io/eng-practices/docs/review/">https://jimmysong.io/eng-practices/docs/review/</a></li>
</ul>
<h2 id="JavaScript-输出语句"><a href="#JavaScript-输出语句" class="headerlink" title="JavaScript 输出语句"></a>JavaScript 输出语句</h2><h3 id="弹窗：alert-语句"><a href="#弹窗：alert-语句" class="headerlink" title="弹窗：alert()语句"></a>弹窗：alert()语句</h3><p>我们要学习的第一个语句，就是 alert 语句。</p>
<p>代码举例如下：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">&quot;viewport&quot;</span> <span class="attr">content</span>=<span class="string">&quot;width=device-width, initial-scale=1.0&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="title function_">alert</span>(<span class="string">&#x27;千古壹号&#x27;</span>);</span></span><br><span class="line"><span class="language-javascript">        </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>

<p><strong>alert</strong>（英文翻译为“警报”）的用途：<strong>弹出“警告框”</strong>。它会在弹窗中显示一条信息，并等待用户按下 “OK”。</p>
<p><code>alert(&quot;&quot;)</code>弹窗的效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180116_1735.gif" class="lozad post-image"src="http://img.smyhvae.com/20180116_1735.gif"></p>
<p>这个弹窗，在 IE 浏览器中长这样：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180116_1906.png" class="lozad post-image"src="http://img.smyhvae.com/20180116_1906.png"></p>
<p>上面的代码中，如果写了两个 alert()语句，则网页的效果是：弹出第一个警告框，点击确定后，继续弹出第二个警告框。</p>
<h3 id="弹窗：confirm-语句（含确认-x2F-取消）"><a href="#弹窗：confirm-语句（含确认-x2F-取消）" class="headerlink" title="弹窗：confirm()语句（含确认&#x2F;取消）"></a>弹窗：confirm()语句（含确认&#x2F;取消）</h3><p>代码举例如下：</p>
<pre class="highlight"><span class="line">var result = confirm(&#x27;你听说过千古壹号吗？&#x27;);</span><br><span class="line">console.log(result);</span><br></pre>

<p>代码运行后，页面上会显示一个弹窗。弹窗上有“确认”和“取消”两个按钮，点击“确定”返回 <code>true</code>，点击“取消”返回 <code>false</code>。</p>
<p>效果如下：</p>
<p><img src="http://img.smyhvae.com/20211031-1537.gif" alt="20211031-1537" srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20211031-1537.gif" class="lozad post-image"></p>
<h3 id="弹出输入框：prompt-语句"><a href="#弹出输入框：prompt-语句" class="headerlink" title="弹出输入框：prompt()语句"></a>弹出输入框：prompt()语句</h3><p><code>prompt()</code>就是专门弹出能够让用户输入的对话框。用得少，测试的时候偶尔会用。</p>
<p>JS 代码如下：</p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> a = <span class="title function_">prompt</span>(<span class="string">&#x27;请随便输入点什么东西吧&#x27;</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(a);</span><br></pre>

<p>上方代码中，用户输入的内容，将被传递到变量 a 里面，并在控制台打印出来。</p>
<p>效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180116_2230.gif" class="lozad post-image"src="http://img.smyhvae.com/20180116_2230.gif"></p>
<p><strong>alert()和 prompt()的区别：</strong></p>
<ul>
<li>alert() 语句中可以输出数字和字符串，如果要输出字符串，则必须用引号括起来；prompt()语句中，用户不管输入什么内容，都是字符串。</li>
<li>prompt() 会返回用户输入的内容。我们可以用一个变量，来接收用户输入的内容。</li>
</ul>
<h3 id="网页内容区域输出：document-write-语句"><a href="#网页内容区域输出：document-write-语句" class="headerlink" title="网页内容区域输出：document.write()语句"></a>网页内容区域输出：document.write()语句</h3><p>代码举例：</p>
<pre class="highlight"><span class="line">document.write(&#x27;千古前端图文教程&#x27;);</span><br></pre>

<p>页面效果：</p>
<p><img src="http://img.smyhvae.com/20211031_1543.png" alt="20211031_1543" srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20211031_1543.png" class="lozad post-image"></p>
<h3 id="控制台输出：console-log-打印日志"><a href="#控制台输出：console-log-打印日志" class="headerlink" title="控制台输出：console.log() 打印日志"></a>控制台输出：console.log() 打印日志</h3><p><code>console.log()</code>表示在控制台中输出。console 表示“控制台”，log 表示“输出”。括号里可以写字符串、数字、变量。</p>
<p>控制台是程序员调试程序的地方，比如使用 console 语句打印日志，测试程序是否运行正常。</p>
<p>在 Chrome 浏览器中，按 F12 即可打开控制台，选择「console」栏，即可看到打印的内容。</p>
<p><code>console.log(&quot;&quot;)</code>效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180116_2008.gif" class="lozad post-image"src="http://img.smyhvae.com/20180116_2008.gif"></p>
<p>console 语句可以设置不同的打印等级：</p>
<pre class="highlight"><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;千古壹号1&#x27;</span>); <span class="comment">//普通打印</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">warn</span>(<span class="string">&#x27;千古壹号2&#x27;</span>); <span class="comment">//警告打印</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">error</span>(<span class="string">&#x27;千古壹号3&#x27;</span>); <span class="comment">//错误打印</span></span><br></pre>

<p>效果如下：</p>
<p><img src="https://img.smyhvae.com/20211031_1552.png" alt="20211031_1552" srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://img.smyhvae.com/20211031_1552.png" class="lozad post-image"></p>
<p>上图中，不同的打印等级，区别不大，只是颜色背景上的区别，方便肉眼区分、过滤信息。</p>
<p>普通人是不会在意控制台的，但是有些网站另藏玄机。比如百度首页的控制台，悄悄地放了一段招聘信息的彩蛋，挺有意思：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180116_2010.png" class="lozad post-image"src="http://img.smyhvae.com/20180116_2010.png"></p>
<p>做前端开发时需要经常使用控制台做调试，我们甚至可以直接在控制台输入 JS 语句，然后打印执行结果。</p>
<p><strong>总结</strong>：alert() 主要用来显示消息给用户，console.log() 用来给程序员做调试用。</p>
<h2 id="赞赏作者"><a href="#赞赏作者" class="headerlink" title="赞赏作者"></a>赞赏作者</h2><p>创作不易，你的赞赏和认可，是我更新的最大动力：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://img.smyhvae.com/20220401_1800.jpg" class="lozad post-image"src="https://img.smyhvae.com/20220401_1800.jpg"></p>

  </div>
  <div>
    
      <div 
        class="post-note note-warning copyright" 
        style="margin-top: 42px">
        <p>
          <span style="font-weight: bold;">作者：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="/about">
            zwl5670
          </a>
        </p>
        <p>
          <span style="font-weight: bold;">文章链接：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="https://zwl5670.gitee.io/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/02-%E5%BC%80%E5%A7%8B%E5%86%99JavaScript%EF%BC%9Ahello%20world/">
            https://zwl5670.gitee.io/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/02-%E5%BC%80%E5%A7%8B%E5%86%99JavaScript%EF%BC%9Ahello%20world/
          </a>
        </p>
        <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
      </div>
    
  </div>
</article>
<div class="nav">
  
    <div class="nav-item-prev">
      <a 
        href="/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/00-%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80%E7%AE%80%E4%BB%8B/" 
        class="nav-link">
        <i class="iconfont icon-left nav-prev-icon"></i>
        <div>
          <div class="nav-label">上一篇</div>
          
            <div class="nav-title">00-编程语言简介 </div>
          
        </div>
      </a>
    </div>
  
  
    <div class="nav-item-next">
      <a 
        href="/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/01-JavaScript%E7%AE%80%E4%BB%8B/" 
        class="nav-link">
        <div>
          <div class="nav-label">下一篇</div>
          
            <div class="nav-title">01-JavaScript简介 </div>
          
        </div>
        <i class="iconfont icon-right nav-next-icon"></i>
      </a>
    </div>
  
</div>

<div 
  class="card card-content toc-card" 
  id="mobiletoc">
  <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%80%E5%A7%8B%E5%86%99%E7%AC%AC%E4%B8%80%E8%A1%8C-JavaScript%EF%BC%9Ahello-world"><span class="toc-text">开始写第一行 JavaScript：hello world</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F-1%EF%BC%9A%E8%A1%8C%E5%86%85%E5%BC%8F"><span class="toc-text">方式 1：行内式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F-2%E3%80%81%E5%86%85%E5%B5%8C%E5%BC%8F%EF%BC%88%E5%86%85%E8%81%94%E5%BC%8F%EF%BC%89"><span class="toc-text">方式 2、内嵌式（内联式）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F-3%EF%BC%9A%E5%A4%96%E9%93%BE%E5%BC%8F"><span class="toc-text">方式 3：外链式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%B3%E4%BA%8E-window-onload%EF%BC%9A%E5%85%88%E5%8A%A0%E8%BD%BD%EF%BC%8C%E6%9C%80%E5%90%8E%E6%89%A7%E8%A1%8C"><span class="toc-text">关于 window.onload：先加载，最后执行</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JavaScript-%E4%B8%80%E4%BA%9B%E7%AE%80%E5%8D%95%E7%9A%84%E8%AF%AD%E6%B3%95%E8%A7%84%E5%88%99"><span class="toc-text">JavaScript 一些简单的语法规则</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%89%8D%E7%AB%AF%E4%BB%A3%E7%A0%81%E7%9A%84%E6%B3%A8%E9%87%8A"><span class="toc-text">前端代码的注释</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#HTML-%E7%9A%84%E6%B3%A8%E9%87%8A"><span class="toc-text">HTML 的注释</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#CSS-%E7%9A%84%E6%B3%A8%E9%87%8A"><span class="toc-text">CSS 的注释</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#JavaScript-%E7%9A%84%E6%B3%A8%E9%87%8A"><span class="toc-text">JavaScript 的注释</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AF%B9%E6%B3%A8%E9%87%8A%E7%9A%84%E8%AE%A4%E7%9F%A5"><span class="toc-text">对注释的认知</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JavaScript-%E8%BE%93%E5%87%BA%E8%AF%AD%E5%8F%A5"><span class="toc-text">JavaScript 输出语句</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%B9%E7%AA%97%EF%BC%9Aalert-%E8%AF%AD%E5%8F%A5"><span class="toc-text">弹窗：alert()语句</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%B9%E7%AA%97%EF%BC%9Aconfirm-%E8%AF%AD%E5%8F%A5%EF%BC%88%E5%90%AB%E7%A1%AE%E8%AE%A4-x2F-%E5%8F%96%E6%B6%88%EF%BC%89"><span class="toc-text">弹窗：confirm()语句（含确认&#x2F;取消）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%B9%E5%87%BA%E8%BE%93%E5%85%A5%E6%A1%86%EF%BC%9Aprompt-%E8%AF%AD%E5%8F%A5"><span class="toc-text">弹出输入框：prompt()语句</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BD%91%E9%A1%B5%E5%86%85%E5%AE%B9%E5%8C%BA%E5%9F%9F%E8%BE%93%E5%87%BA%EF%BC%9Adocument-write-%E8%AF%AD%E5%8F%A5"><span class="toc-text">网页内容区域输出：document.write()语句</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8E%A7%E5%88%B6%E5%8F%B0%E8%BE%93%E5%87%BA%EF%BC%9Aconsole-log-%E6%89%93%E5%8D%B0%E6%97%A5%E5%BF%97"><span class="toc-text">控制台输出：console.log() 打印日志</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%B5%9E%E8%B5%8F%E4%BD%9C%E8%80%85"><span class="toc-text">赞赏作者</span></a></li></ol>
</div>
            </main>
            <aside class="right-column">
              <div class="sticky-widescreen">
  
  
    <article class="card card-content toc-card">
      <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%80%E5%A7%8B%E5%86%99%E7%AC%AC%E4%B8%80%E8%A1%8C-JavaScript%EF%BC%9Ahello-world"><span class="toc-text">开始写第一行 JavaScript：hello world</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F-1%EF%BC%9A%E8%A1%8C%E5%86%85%E5%BC%8F"><span class="toc-text">方式 1：行内式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F-2%E3%80%81%E5%86%85%E5%B5%8C%E5%BC%8F%EF%BC%88%E5%86%85%E8%81%94%E5%BC%8F%EF%BC%89"><span class="toc-text">方式 2、内嵌式（内联式）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F-3%EF%BC%9A%E5%A4%96%E9%93%BE%E5%BC%8F"><span class="toc-text">方式 3：外链式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%B3%E4%BA%8E-window-onload%EF%BC%9A%E5%85%88%E5%8A%A0%E8%BD%BD%EF%BC%8C%E6%9C%80%E5%90%8E%E6%89%A7%E8%A1%8C"><span class="toc-text">关于 window.onload：先加载，最后执行</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JavaScript-%E4%B8%80%E4%BA%9B%E7%AE%80%E5%8D%95%E7%9A%84%E8%AF%AD%E6%B3%95%E8%A7%84%E5%88%99"><span class="toc-text">JavaScript 一些简单的语法规则</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%89%8D%E7%AB%AF%E4%BB%A3%E7%A0%81%E7%9A%84%E6%B3%A8%E9%87%8A"><span class="toc-text">前端代码的注释</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#HTML-%E7%9A%84%E6%B3%A8%E9%87%8A"><span class="toc-text">HTML 的注释</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#CSS-%E7%9A%84%E6%B3%A8%E9%87%8A"><span class="toc-text">CSS 的注释</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#JavaScript-%E7%9A%84%E6%B3%A8%E9%87%8A"><span class="toc-text">JavaScript 的注释</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AF%B9%E6%B3%A8%E9%87%8A%E7%9A%84%E8%AE%A4%E7%9F%A5"><span class="toc-text">对注释的认知</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JavaScript-%E8%BE%93%E5%87%BA%E8%AF%AD%E5%8F%A5"><span class="toc-text">JavaScript 输出语句</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%B9%E7%AA%97%EF%BC%9Aalert-%E8%AF%AD%E5%8F%A5"><span class="toc-text">弹窗：alert()语句</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%B9%E7%AA%97%EF%BC%9Aconfirm-%E8%AF%AD%E5%8F%A5%EF%BC%88%E5%90%AB%E7%A1%AE%E8%AE%A4-x2F-%E5%8F%96%E6%B6%88%EF%BC%89"><span class="toc-text">弹窗：confirm()语句（含确认&#x2F;取消）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%B9%E5%87%BA%E8%BE%93%E5%85%A5%E6%A1%86%EF%BC%9Aprompt-%E8%AF%AD%E5%8F%A5"><span class="toc-text">弹出输入框：prompt()语句</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BD%91%E9%A1%B5%E5%86%85%E5%AE%B9%E5%8C%BA%E5%9F%9F%E8%BE%93%E5%87%BA%EF%BC%9Adocument-write-%E8%AF%AD%E5%8F%A5"><span class="toc-text">网页内容区域输出：document.write()语句</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8E%A7%E5%88%B6%E5%8F%B0%E8%BE%93%E5%87%BA%EF%BC%9Aconsole-log-%E6%89%93%E5%8D%B0%E6%97%A5%E5%BF%97"><span class="toc-text">控制台输出：console.log() 打印日志</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%B5%9E%E8%B5%8F%E4%BD%9C%E8%80%85"><span class="toc-text">赞赏作者</span></a></li></ol>
    </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header">
    <i 
      class="iconfont icon-wenzhang_huaban" 
      style="padding-right: 2px;">
    </i>最近文章
  </div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/11-React%20Navive%E5%88%9D%E8%AF%86/"><div class="recent-posts-item-content">11-React Navive初识</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/10-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E4%BD%BF%E7%94%A8customRequest%E6%96%B9%E6%B3%95%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%8A%E4%BC%A0%E8%A1%8C%E4%B8%BA/"><div class="recent-posts-item-content">10-AntD框架的upload组件上传图片时使用customRequest方法自定义上传行为</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/09-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E9%81%87%E5%88%B0%E7%9A%84%E4%B8%80%E4%BA%9B%E5%9D%91/"><div class="recent-posts-item-content">09-AntD框架的upload组件上传图片时遇到的一些坑</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/08-Ant%20Design%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/"><div class="recent-posts-item-content">08-Ant Design的基本使用</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
          </div>
        </div>
      </div>
    </div>
     
    <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>
          Copyright ©
          
            2023
          
          
        </span>
        &nbsp;
        <a 
          href="/" 
          class="footer-link">
          前端面试
        </a>
      </div>
    </div>

    
      <div class="footer-dsc">
        
          Powered by
          <a 
            href="https://hexo.io/" 
            class="footer-link" 
            target="_blank" 
            rel="nofollow noopener noreferrer">
            &nbsp;Hexo
          </a>
        
        
          <span>&nbsp;|&nbsp;</span>
        
        
          Theme -
          <a 
            href="https://github.com/theme-kaze" 
            class="footer-link" 
            target="_blank"
            rel="nofollow noopener noreferrer">
            &nbsp;Kaze
          </a>
        
      </div>
    
    
    
    
</footer>
 
    
  <a 
    role="button" 
    id="scrollbutton" 
    class="basebutton" 
    aria-label="回到顶部">
    <i class="iconfont icon-arrowleft button-icon"></i>
  </a>

<a 
  role="button" 
  id="menubutton"
  aria-label="menu button"
  class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a 
  role="button" 
  id="popbutton" 
  class="basebutton" 
  aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a 
  role="button" 
  id="darkbutton" 
  class="basebutton darkwidget" 
  aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a 
  role="button" 
  id="searchbutton" 
  class="basebutton searchwidget" 
  aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a> 
     
     
     
      <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img')
    var i
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a')
      wrapper.setAttribute('href', img[i].getAttribute('data-src'))
      wrapper.setAttribute('aria-label', 'illustration')
      wrapper.style.cssText =
        'width: 100%; display: flex; justify-content: center;'
      if (img[i].alt) wrapper.dataset.caption = img[i].alt
      wrapper.dataset.nolink = true
      img[i].before(wrapper)
      wrapper.append(img[i])
      var divWrap = document.createElement('div')
      divWrap.classList.add('gallery')
      wrapper.before(divWrap)
      divWrap.append(wrapper)
    }
    baguetteBox.run('.gallery')
  }
</script>
<script>
  loadScript(
    "/js/lib/lightbox/baguetteBox.min.js",
    addImgLayout
  )
</script>
 
     
     
    <script src="/js/main.js"></script> 
     
    
      <script>
        var addLazyload = function () {
          var observer = lozad('.lozad', {
            load: function (el) {
              el.srcset = el.getAttribute('data-src')
            },
            loaded: function (el) {
              el.classList.add('loaded')
            },
          })
          observer.observe()
        }
      </script>
      <script>
        loadScript('/js/lib/lozad.min.js', addLazyload)
      </script>
    
    <script src="//instant.page/5.1.0" type="module"
      integrity="sha384-by67kQnR+pyfy8yWP4kPO12fHKRLHZPfEsiSXR8u2IKcTdxD805MGUXBzVPnkLHw"></script>
    
    
  </body>
</html>
